<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/1/26
 * Time: 17:14
 * Desc: 重新调整会员中心类
 */
namespace app\index\controller;
use think\Controller;
use think\Db;
use app\index\controller\Base;
use app\index\service\UserService;
use app\index\service\UserOtherService;
use app\index\service\WeixinService;
use pinyin\Pinyin;

class User extends Base {
    //当前选择的城市
    public $city = '';


    public function _initialize()
    {
        parent::_initialize();
        if(empty(session('_user_uid_.uid'))){
            $url = url('Login/loginin')."?c=user&a=index";       //登陆完成跳转回来的url
            $this->redirect($url);       //登录
        }
        $this->assign('realname',session('_user_uid_.realname'));
    }

    /**
     * @Author  lingyun
     * @Desc    判断当前的城市是否
     */
    private function get_city_stores(){
        $pinyin = new Pinyin();
        $post['key'] = $pinyin->getAllPY($post['value']);
//        $result = db('')
    }

    /**
	 * 个人会员的首页
	 * @return [type] [description]
	 */
	public function index(){
	    if(empty(session('_user_uid_.uid'))){
	        $this->redirect('Login/loginin');
        }

        $UserService = new UserService();
        $UserService->get_user_info();              //获取会员的信息
        $UserService->index_order_nav();            //会员中心订单导航
        $UserService->user_index_coupon_nav();      //会员优惠券功能完善
        $UserService->my_order();                   //我的订单
        $UserService->get_header();                 //获取头部信息
        $status = input('status');
        $this->assign('cur_lanmu',1);

        $this->assign('city',session('city'));                //当前选择的城市
		return view('index');
	}

    /**
     * @Author  lingyun
     * @Desc    我的订单信息
     */
	public function my_order(){
	    $userservice = new UserService();
        $userservice->my_order();
        $userservice->get_header();
        $userservice->get_user_info();              //获取会员的信息
        $this->assign('cur_lanmu',1);
        return $this->fetch('my_order');
    }

    /**
     * @Author  lingyun
     * @Desc    订单详情
     */
    public function order_detail(){
        $userservice = new UserService();
        $order = db('order')->where('id',input('id'))->find();
        if(empty($order)){      //订单为空
            $this->assign('err_msg','找不到订单信息');
            $this->assign('err_url',url('User/user_credit_log'));
            $this->assign('err_button','返回');
            return $this->fetch('public/error');
            exit();
        }
        $userservice->get_user_info();              //获取会员的信息
        $userservice->order_detail();
        $userservice->get_header();
        $this->assign('cur_lanmu',1);
        return $this->fetch('order_detail');
    }

    /**
     * @Author  lingyun
     * @Desc    订单重新支付
     */
    public function create_repay_order(){
        $userservice = new UserService();
        $result = $userservice->create_repay_order();         //创建重新支付订单
        $store_id = cookie('store_id');
        $store = db('store')->where('id',$store_id)->field('id,mobile,address,is_jiameng')->find();
        if($result['code'] == 0){
            $this->assign('err_msg',$result['err_msg']);
            $this->assign('err_url',$result['err_url']);
            $this->assign('err_button',$result['err_button']);
            return $this->fetch('public/error');
            exit();
        }

        $this->assign('store_info',$store);
        return $this->fetch('repay_order');
    }


    /**
     * @Author  lingyun
     * @Desc    重新支付订单
     */
    public function repay_order(){
        $post = input('post.');
        $userservice = new UserService();
        $result = $userservice->repay_order($post);        //重新支付订单
        if($result['code'] == 0){
            $this->assign('err_msg',$result['err_msg']);
            $this->assign('err_url',$result['err_url']);
            $this->assign('err_button',$result['err_button']);
            return $this->fetch('public/error');
        }else{
            if($post['pay_type'] == 1){             //支付宝支付
                $this->redirect(url('Alipay/pay').'?order_sn='.$result['order_sn']);
            }else if($post['pay_type'] == 2){       //微信支付
                $this->redirect(url('Weixinpay/weixin_pay').'?order_sn='.$result['order_sn']);
//            $this->redirect('Weixinpay/weixin_pay?order_sn='.$order_sn.'&final_amount='.$post['final_amount']);
            }else{                                  //余额支付
                $this->redirect(url('User/pay_order_by_credit').'?order_sn='.$result['order_sn']);
            }
        }
    }

    /**
     * @Author  lingyun
     * @Desc    余额支付订单
     * 2018-05-01
     */
    public function pay_order_by_credit(){
        $uid = session('_user_uid_.uid');
        $member = db('user')->where('uid',$uid)->find();
        $order_sn = input('order_sn');
        $order = db('order')->where('order_sn',$order_sn)->find();
        $store = db('store')->where('id',$order['store_id'])->find();

        if(empty($member)){
            $this->assign('err_msg','您目前还没登录，请先登录');
            $this->assign('err_url',url('Login/loginin'));
            $this->assign('err_button','登录');
            return $this->fetch('public/error');
        }

        if($member['user_money'] < $order['final_amount']){
            $this->assign('err_msg','你的余额已不足，不能发起支付');
            $this->assign('err_url',url('User/index'));
            $this->assign('err_button','登录');
            return $this->fetch('public/error');
        }

        if(empty($order_sn) || empty($order)){
            $this->assign('err_msg','找不到该订单信息，不能发起支付');
            $this->assign('err_url',url('User/index'));
            $this->assign('err_button','查看订单');
            return $this->fetch('public/error');
        }

        if($order['addtime']+864000 < time()){
            $this->assign('err_msg','该订单已关闭，不能发起支付');
            $this->assign('err_url',url('User/index'));
            $this->assign('err_button','查看订单');
            return $this->fetch('public/error');
        }

        if($order['pay_status'] == 0){
            //构造余额支付记录数据
            $data_arr = [
                'uid'=>$uid,
                'admin_id'=>0,
                'store_id'=>$order['store_id'],
                'order_id'=>$order['id'],
                'order_sn'=>$order['order_sn'],
                'pay_time'=>time(),
                'pay_status'=>1,
                'pay_type'=>3,
                'amount'=>$order['final_amount'],
                'cur_amount'=>$member['user_money']-$order['final_amount'],
                'sign'=>2,
                'chargetype'=>1,
                'charge_cn'=>'线上购买商品，订单号'.$order['order_sn'].'，总价：'.$order['final_amount'],
                'addtime'=>time()
            ];
            //记录余额记录
            db('user_wallet_log')->data($data_arr)->insert();       //记录余额记录

            $pay_log_arr = [
                'order_id'=>$order['id'],
                'order_sn'=>$order['order_sn'],
                'order_type'=>1,
                'uid'=>$uid,
                'pay_type'=>3,
                'pay_sign'=>1,
                'store_id'=>$order['store_id'],
                'amount'=>$order['final_amount'],
                'remark'=>'线上购买商品，订单号'.$order['order_sn'].'，总价：'.$order['final_amount'],
                'addtime'=>time()
            ];
            db('pay_log')->data($pay_log_arr)->insert();            //记录支付记录

            //更新需要向门店进行转账的金额
            $store_amount_arr = [
                'total_amount'=>$store['total_amount']+$order['final_amount'],
                'credit'=>$store['credit']+$order['final_amount'],
                'user_money'=>$store['user_money']+$order['final_amount']
            ];
            db('store')->where('id',$order['store_id'])->update($store_amount_arr);


            $store_credit_log = [
                'store_id' => $order['store_id'],
                'order_id' => $order['id'],
                'type' => 1,
                'credit' => $order['final_amount'],
                'cur_credit' => $store['total_amount'] + $order['final_amount'],
                'remark' => '销售商品收入',
                'addtime' => time(),
                'order_type' => 1,
                'order_sn' => $order['order_sn'],
                'pay_type' => 3
            ];

            db('store_credit_log')->insert($store_credit_log);


            $cur_amount = $member['user_money'] - $order['final_amount'];    //会员当前的余额
            db('user')->where('uid',$uid)->update(['user_money'=>$cur_amount]);     //更新会员当前的余额

            if($order['is_ziqu'] == 1){         //自取订单直接完成
                db('order')->where('order_sn',$order_sn)->update(['pay_status'=>1,'pay_time'=>time(),'order_status'=>4,'remark'=>'已支付']);      //更新订单支付状态
                db('order_details')->where('order_id',$order['id'])->update(['status'=>4]);     //更新订单详情的支付状态
            }else{
                db('order')->where('order_sn',$order_sn)->update(['pay_status'=>1,'pay_time'=>time(),'order_status'=>1,'remark'=>'已支付']);      //更新订单支付状态
                db('order_details')->where('order_id',$order['id'])->update(['status'=>1]);     //更新订单详情的支付状态
            }

            //更新优惠券的使用情况
            db('user_ycard')->where('id',$order['coupon_id'])->update(['order_id'=>$order['id'],'used_time'=>time(),'is_used'=>1]);
            db('user_ycard')->where('id',$order['cash_coupon_id'])->update(['order_id'=>$order['id'],'used_time'=>time(),'is_used'=>1]);

            //更新门店库存
            $order_details = db('order_details')->where('order_id',$order['id'])->select();
            foreach($order_details as $k => $v){
//                db('store_goods_stock')->where('id',$v['store_goods_stock_id'])->setDec('stock',$v['goods_num']);

                $stock = db('store_goods_stock')->where('id',$v['store_goods_stock_id'])->find();
                $cur_stock = $stock['stock'] - $v['goods_num'];
                //更新门店库存
                db('store_goods_stock')->where('id',$v['store_goods_stock_id'])->update(['stock'=>$cur_stock]);
                $arr = [
                    'goods_id'=>$v['goods_id'],
                    'goods_spec_id'=>$stock['goods_spec_id'],
                    'title'=>$v['spec_detail'],
                    'order_id'=>$order['id'],
                    'store_id'=>$order['store_id'],
                    'types'=>2,
                    'ruku_num'=>0,
                    'tuihuo_num'=>0,
                    'chuku_num'=>$v['goods_num'],
                    'sunhao_num'=>0,
                    'remark'=>'售卖'.$v['goods_num'],
                    'cur_stock'=>$cur_stock,
                    'addtime'=>time(),
                    'order_sn'=>$order['order_sn']
                ];
                db('store_goods_log')->insert($arr);

            }

            //订单动作数据
            $data_action = [
                'uid'=>$uid,
                'order_id'=>$order['id'],
                'action_user'=>0,
                'order_status'=>1,
                'pay_status'=>1,
                'status_desc'=>'已支付，待配送',
                'storeid'=>$order['store_id'],
                'addtime'=>time()
            ];
            db('order_action')->data($data_action)->insert();       //新增订单数据

            //支付回调进行三级分销积分变更
            third_tier_distribution($uid,$order['final_amount'],$order['store_id'],$order['id'],0,$order['order_sn']);

            //存在会员的openid就推送模板消息
            if(!empty($member['openid'])){
                //推送模板消息
                $weixin_msg = [
                    "type"   	  =>1,
                    "touser" 	  =>$member["openid"],
                    "template_id"=>config("mobanid.xiadan")["tempid"],
                    "url" 		  =>config("mobanid.xiadan")["url"],
                    "topcolor"  =>"#743A3A",
                    "data"   =>[
                        "first" 	    =>["value"=>"支付成功",        		"color"=>"#743A3A"],
                        "keyword1"		=>["value"=>$order['order_sn'], 		"color"=>"#743A3A"],
                        "keyword2"		=>["value"=>$order['final_amount'], 		"color"=>"#743A3A"],
                        "keyword3"		=>["value"=>"微信支付","color"=>"#743A3A"],
                        "remark"		=>["value"=>"感谢您的购买，请注意查收商品", 			"color"=>"#743A3A"],
                    ]
                ];
                $wein = new WeixinService();
                $rest  = $wein->sendmsg($weixin_msg);
            }
        }

        if($order['receive_coupon_id'] != ''){
            $this->assign('result',1);
        }else{
            $this->assign('result',0);
        }
        $service = new UserService();
        $service->get_header();             //获取顶部信息
//        halt($order['receive_coupon_id']);
        $my_coupon = db('user_ycard')->where(['uid'=>$uid,'store_id'=>cookie('store_id')])->column('coupon_id');
        $coupon = db('store_coupon')->where('id','in',trim($order['receive_coupon_id'],','))->select();
        $this->assign('coupon',$coupon);
        $this->assign('my_coupon',$my_coupon);
        $this->assign('receive_order_id',$order['id']);
        $this->assign('point',floor($order['final_amount']/2));
        return $this->fetch('zengsong_coupon');

        //支付成功，跳转至领取优惠券页面
        $this->success('支付成功',url('PayComplete/get_coupon')."?out_trade_no=".$order_sn,3);
    }

    /**
     * @Author  lingyun
     * @Desc    删除订单 -- 删除未支付的订单
     */
    public function del_order(){
        $userservice = new UserService();
        $userservice->del_order();
    }

    /**
     * @Author  lingyun
     * @Desc    取消订单
     */
    public function cancel_order(){
        $userservice = new UserService();
        $userservice->cancel_order();
    }

    /**
     * @Author  lingyun
     * @Desc    商品评价列表
     * 2018-05-02
     */
    public function order_pingjia_list(){
        $userservice = new UserService();
        $userservice->get_header();
        $userservice->get_user_info();
        $userservice->pingjia_order_detail();
        $this->assign('cur_lanmu',1);
        return $this->fetch();
    }

    /**
     * @Author  lingyun
     * @Desc    评价商品
     * 2018-05-03
     */
    public function pingjia_goods(){
        $userservice = new UserService();
        $request = request();
        if($request->isPost()){
            $userservice->comment_order_goods();
        }
        $id = input('id');
        $info = db('order_details')->alias('a')->join('ys_goods b','a.goods_id=b.id','LEFT')->where('a.id',$id)->field('a.*,b.logo,b.name')->find();
        $userservice->get_header();
        $userservice->get_user_info();
        $userservice->pingjia_goods_details();
        $this->assign('info',$info);
        $this->assign('cur_lanmu',1);
        return $this->fetch();

    }

    /**
     * @Author  lingyun
     * @Desc    确认收货
     */
    public function confirm_receive_order(){
        $userservice = new UserService();
        $userservice->confirm_receive_order();
    }

    /**
     * @Author  lingyun
     * @Desc    订单评论 -- 评论购买的商品
     */
    public function comment_order_goods(){
        $post = input('param.');
        $order_detail = db('order_details')->alias('a')->join('ys_goods b','a.goods_id=b.id','LEFT')->where('a.order_id',$post['order_id'])->field('a.id,a.spec_detail,a.is_comment,b.name,b.logo')->select();
        $this->assign('order_id',$post['order_id']);
        $this->assign('order_detail',$order_detail);
        $this->assign('cur_lanmu',1);
        return $this->fetch('comment_order_goods');
    }

//    /**
//     * @Author  lingyun
//     * @Desc    提交订单评价
//     */
//    public function post_comment(){
//        $request = request();
//        if($request->isPost()){
//            $userservice = new UserService();
//            $result = $userservice->comment_order_goods();
//            if($result['code'] == 1){
//                $this->success('评价成功',url('User/index'));
//            }else{
//                $this->error('评价失败',url('User/index'));
//            }
//        }else{
//            $post = input('param.');
//            $this->assign('id',$post['id']);        //order_details 的主键id
//            return $this->fetch('post_comment');
//        }
//    }

    /**
     * @Author  lingyun
     * @Desc    我的收货地址
     */
    public function my_address(){
        $userotherservice = new UserOtherService();
        $userotherservice->my_address();
        $this->assign('cur_lanmu',3);
        return $this->fetch('my_address');
    }

    /**
     * @Author  lingyun
     * @Desc    添加 -- 编辑收货地址
     */
    public function add_edit_address(){
        $userotherservice = new UserOtherService();
        $userotherservice->add_edit_address();
        $this->assign('cur_lanmu',3);
        return $this->fetch('add_edit_address');
    }

    /**
     * @Author  lingyun
     * @Desc    设置默认地址
     */
    public function set_address_default(){
        $userotherservice = new UserOtherService();
        $userotherservice->set_address_default();
    }

    /**
     * @Author  lingyun
     * @Desc    取消设置默认地址
     */
    public function cancel_address_default(){
        $userotherservice = new UserOtherService();
        $userotherservice->cancel_address_default();
    }
    /**
     * @Author  lingyun
     * @Desc    我的优惠券
     */
    public function my_coupon(){
        $userotherservice = new UserOtherService();
        $userservice = new UserService();
        $userotherservice->my_coupon();
        $userservice->get_header();
        $userservice->get_user_info();
        $this->assign('cur_lanmu',2);

        return $this->fetch('my_coupon');
    }

    /**
     * @Author  lingyun
     * @Desc    领取优惠券
     */
    public function receive_coupon(){
        $userotherservice = new UserOtherService();
        $userotherservice->receive_coupon();
    }

    /**
     * @Author  lingyun
     * @Desc    我的收藏 -- 商品
     */
    public function my_collect(){
        $userotherservice = new UserOtherService();
        $userservice = new UserService();
        $userotherservice->my_collect();
        $userservice->get_header();
        $userservice->get_user_info();
        $this->assign('cur_lanmu',6);
        return $this->fetch('my_collect');
    }

    /**
     * @Author  lingyun
     * @Desc    上拉加载获取收藏的数据
     * 2018-05-05
     */
    public function get_ajax_collect(){
        $page = input('cur_page');              //当前页码
        $service = new UserOtherService();
        $service->get_ajax_collect($page);
    }

    /**
     * @Author  lingyun
     * @Desc    取消收藏
     */
    public function cancel_collect(){
        $id = input('id');
        $res = db('user_collect')->where('id',$id)->update(['status'=>0]);
        $res!==false?$this->success('取消成功',''):$this->error('取消失败,请稍后再试','');
    }

    /**
     * @Author  lingyun
     * @Desc    我的邀请二维码
     */
    public function my_invite_qrcode(){
        $root = request()->domain();
        $url = $root."/index.php/index/register/register?sharer=".session('_user_uid_.uid');
        $userotherservice = new UserOtherService();
        $userservice = new UserService();
        $userotherservice->my_invite_qrcode();
        $userservice->get_header();
        $userservice->get_user_info();
        if(is_weixin()){
            $signPackage = $userotherservice->wxShare();
        }else{
            $signPackage = [
                'appId'=>'',
                'timestamp'=>'',
                'nonceStr'=>'',
                'signature'=>''
            ];
        }
        $share = [
            'title'=>'一朵',
            'link'=>$url,
            'desc'=>'一朵',
            'imgUrl'=>'https://bpic.588ku.com/element_pic/18/01/18/0ec8e1a86e988ae4442dad932074415d.jpg!/fw/235/quality/90/unsharp/true/compress/true'
        ];
        $this->assign('signPackage',$signPackage);
        $this->assign('share',$share);
        $this->assign('cur_lanmu',1);
        return $this->fetch('my_invite_qrcode');
    }

    /**
     * @Author  lingyun
     * @Desc    我的支付码 -- 条形码
     * @return \think\response\View
     */
    public function my_paycode(){
        $UserService = new UserService();
        $UserService->get_user_info();              //获取会员的信息
        $UserService->get_header();                 //获取头部信息

        $status = input('status');
        $uid = session('_user_uid_.uid');
        $user = db('user')->where('uid',$uid)->find();
        $this->assign('user',$user);
        $this->assign('cur_lanmu',1);
        return $this->fetch('my_paycode');
    }

    /**
     * @Author  lingyun
     * @Desc    生成条形码图片
     * @param   pay_type 1:支付宝  2微信
     * @param   生成12位
     * 2018-03-21
     */
    public function create_paycode(){
        $uid = session('_user_uid_.uid');
        $pay_code = db('user')->where('uid',$uid)->value('pay_code');
        $userotherservice = new UserOtherService();
//        $userotherservice->pay_code('123456578342');
        $userotherservice->pay_code($pay_code);
    }

    /**
     * @Author  lingyun
     * @Desc    会员充值
     *          支付方式：支付方式，1支付宝，2微信，3余额 ，4现金，5银行卡
     * 2018-03-21
     */
    public function recharge(){
        $request = request();
        $userotherservice = new UserOtherService();
        $userservice = new UserService();
        if($request->isPost()){
            $post = input('post.');
            if(session('_user_uid_.uid') == ''){
                $this->assign('err_msg','您目前没有登录，请先去登录');
                $this->assign('err_url',url('Login/loginin'));
                $this->assign('err_button','登录');
                return $this->fetch('public/error');
                exit();
            }
            $result = $userotherservice->pre_pay_recharge($post);
            if($result['code'] == 0){
                $this->assign('err_msg',$result['err_msg']);
                $this->assign('err_url',$result['err_url']);
                $this->assign('err_button',$result['err_button']);
                return $this->fetch('public/error');
                exit();
            }else{
                if($post['pay_type'] == 1){     //支付宝
                    $this->redirect(url('Alipay/recharge').'?order_sn='.$result['order_sn']);
                }else if($post['pay_type'] == 2){
                    if(is_mobile() && !is_weixin()){
                        $this->redirect(url('Wxpaymobile/h5pay_recharge').'?order_sn='.$result['order_sn']);
                    }
                    $this->redirect(url('Weixinpay/recharge').'?order_sn='.$result['order_sn']);
                }
            }
        }else{
            $userotherservice->get_recharge_type();
            $userservice->get_header();
            $userservice->get_user_info();              //获取会员的信息
            if(isMobile()){
                if(is_weixin()){
                    $this->assign('is_mobile',0);
                }else{
                    $this->assign('is_mobile',1);
                }
            }else{
                $this->assign('is_mobile',0);
            }

            if(is_weixin()){
                $this->assign('is_weixin',1);
            }else{
                $this->assign('is_weixin',0);
            }
            $this->assign('cur_lanmu',1);
            return $this->fetch();
        }


//        $request = request();
//        $member = db('user')->where('uid',session('_user_uid_.uid'))->field('uid,nickname,mobile,user_money')->find();  //会员信息
//        if($request->isPost()){
//            $post = input('post.');
//            $reg = '/^\d+(\.\d{1,2})?$/';       //匹配金额
//            $amount = 0;        //充值金额
//
//            if(!empty($post['recharge_type'])){     //快捷充值方式
//                $rule = db('sys_level')->where('id',$post['recharge_type'])->find();
//                $amount = $rule['score'];
//            }else{
//                if(empty($post['amount'])){
//                    $this->error('请输入充值金额',url('Index/recharge'));
//                }
//                $amount = $post['amount'];
//            }
//            if($amount == ''){          //请填写充值金额
//                $this->error('请填写充值金额',url('User/recharge'));
//            }
//            if(empty($post['pay_type'])){           //支付方式
//                $this->error('请选择支付方式',url('User/recharge'));
//            }
//            if(!preg_match($reg,$amount)){      //充值金额有误
//                $this->error('充值金额填写有误，请重新填写',url('User/recharge'));
//            }
//            //订单编号
//            $order_sn = create_order_sn(session('_user_uid_.uid'));
//            //会员充值变更记录
//            $data = [
//                'uid'=>session('_user_uid_.uid'),
//                'store_id'=>0,
//                'order_id'=>0,
//                'order_sn'=>$order_sn,
//                'pay_status'=>0,
//                'pay_type'=>$post['pay_type'],
//                'amount'=>$amount,
//                'give_away_amount'=>0,
//                'cur_amount'=>$member['user_money']+$amount,
//                'sign'=>2,
//                'chargetype'=>2,
//                'charge_cn'=>'余额充值',
//                'addtime'=>time()
//            ];
//            $res = db('user_wallet_log')->insertGetId($data);
//            $res>0?$this->success('保存成功','',$res):$this->error('保存失败','',$res);
//
////            if($post['pay_type'] == 1){     //支付宝充值
////                $this->redirect(url('Alipay/recharge')."?id=".$res);
////            }else if($post['pay_type'] == 2){       //微信支付
////                if(is_weixin()){        //微信端支付
////                    $this->redirect(url('Weixinpay/recharge')."?id=".$res);     //微信支付
////                }else{
////                    $this->redirect(url('Weixinpay/recharge_code')."?id=".$res);        //微信扫码支付
////                }
////            }
//        }
//        $rule = db('sys_level')->where('score','gt',0)->order('score asc')->select();     //充值活动等级规则
//        //是微信端
//        if(is_weixin()){
//            $this->assign('is_weixin',1);
//        }else{
//            $this->assign('is_weixin',0);
//        }
//
//        $this->assign('member',$member);
//        $this->assign('rule',$rule);
//        return $this->fetch('recharge');
    }

    /**
     * @Author  lingyun
     * @Desc    会员余额变动记录
     * 2018-03-21
     */
    public function user_credit_log(){
        $service = new UserOtherService();
        $userservice = new UserService();
        $userservice->get_header();
        $userservice->get_user_info();
        $service->user_credit_log();
        $this->assign('cur_lanmu',10);
        return $this->fetch('user_credit_log');
    }

    /**
     * @Author  lingyun
     * @Desc    ajax查询获取余额记录
     * 2018-05-03
     */
    public function get_ajax_wallet_log(){
        $page = input('cur_page');              //当前页码
        $service = new UserOtherService();
        $service->get_ajax_wallet_log($page);
    }

    /**
     * @Author  lingyun
     * @Desc    会员权益
     * 2018-05-02
     */
    public function quanyi(){
        $userotherservice = new UserOtherService();
        $userservice = new UserService();
        $userservice->get_header();
        $userservice->get_user_info();
        $userotherservice->get_recharge_type();
        $this->assign('cur_lanmu',1);
        return $this->fetch();

    }

    /**
     * @Author      Hybrid
     * @DateTime    2018-05-04
     * @Description 我的积分排行榜
     * @return      [type]     [description]
     *              type   1日榜  2周榜  3月榜  4年榜
     */
    public function my_point_rank(){
        $type = empty(input('type'))?4:input('type');
        $userotherservice = new UserOtherService();
        $userservice = new UserService();
        $userotherservice->get_point_rank($type);
        $userservice->get_header();
        $userservice->get_user_info();              //获取会员的信息
        $this->assign('cur_lanmu',10);
        return $this->fetch('my_point_rank');
    }

    /**
     * @Author  lingyun
     * @Desc    我的积分排行榜
     */
    public function my_point_log(){
        $userservice = new UserService();
        $userotherservice = new UserOtherService();
        $userotherservice->my_point_log();
        $userservice->get_header();
        $userservice->get_user_info();              //获取会员的信息
        $this->assign('cur_lanmu',10);
        return $this->fetch();
    }

    /**
     * @Author  lingyun
     * @Desc    获取ajax加载的积分详情信息
     */
    public function get_ajax_point_log(){
        $page = input('cur_page');              //当前页码
        $userotherservice = new UserOtherService();
        $userotherservice->get_ajax_point_log($page);
    }

    /**
     * @Author  lingyun
     * @Desc    单笔最大消费排行
     */
    public function max_consume_rank(){
        $userservice = new UserService();
        $userservice->get_header();
        $userservice->get_user_info();
        $uid = session('_user_uid_.uid');
        $store_id = cookie('store_id');         //门店
        $store = db('store')->where('id',$store_id)->find();
        $sql = "select 0 as rownum, a.realname,a.logo,a.mobile,a.uid,c.amount as timestampsum from ys_user  as a left join (select max(final_amount) as amount,uid,order_status from ys_order   where pay_status=1 and order_status>=1 group by uid order by amount desc) as c on a.uid=c.uid  where a.is_deleted=0 order by timestampsum desc ";

//        $sql = "SELECT obj.timestampsum,a.uid,a.mobile,a.realname,@rownum := @rownum + 1 AS rownum FROM ys_user as a left join (SELECT uid, max(final_amount) as timestampsum FROM ys_order  group by uid ORDER BY timestampsum DESC ) AS obj on a.uid=obj.uid, (SELECT @rownum := 0) r";

        $uid = session('_user_uid_.uid');
        $list = Db::query($sql);

        foreach($list as $k => $v){
            if($k == 1){
                $list[$k]['rownum'] = 2;
                continue;
            }
            if($k == 2){
                $list[$k]['rownum'] = 3;
                continue;
            }

            if($k != 0){
                $up_k = $k-1;
                if($v['timestampsum'] == $list[$up_k]['timestampsum']){
                    $list[$k]['rownum'] = $list[$up_k]['rownum'];
                }else{
                    $list[$k]['rownum'] = $list[$up_k]['rownum']+1;
                }
            }else{
                $list[$k]['rownum'] = 1;
            }
        }
        $paihang = array_filter($list, function($t) use ($uid) { return $t['uid'] == $uid; });
        $this->assign('list',$list);
        $this->assign('paihang',array_values($paihang));
        $this->assign('store',$store);
        $this->assign('type',1);
        unset($list[0]);
        unset($list[1]);
        unset($list[2]);
        $this->assign('m_list',$list);
        $this->assign('cur_lanmu',5);
        return $this->fetch('my_point_rank');
    }

    /**
     * @Author  lingyun
     * @Desc    PC对岸安全中心
     * @return mixed
     */
    public function my_center(){
        $userservice = new UserService();
        $userotherservice = new UserOtherService();
        if(request()->isPost()){
            $userotherservice->edit_member_info();
        }

        $userservice->get_header();
        $userservice->get_user_info();
        $this->assign('cur_lanmu',4);
        return $this->fetch();
    }

    /**
     * @Author  lingyun
     * @Desc    移动端修改个人信息
     * 2018-05-10
     */
    public function m_edit_member_info(){
        $userservice = new UserService();
        $userotherservice = new UserOtherService();
        if(request()->isPost()){
            $userotherservice->m_edit_member_info();
        }

        $userservice->get_header();
        $userservice->get_user_info();
        $this->assign('cur_lanmu',4);
        return $this->fetch();
    }

    /**
     * @Author  lingyun
     * @Desc    修改密码
     * 2018-05-10
     */
    public function edit_password(){
        $userservice = new UserService();
        $userotherservice = new UserOtherService();

        if(request()->isPost()){
            $userotherservice->edit_password();
        }
        $userservice->get_header();
        $userservice->get_user_info();
        $this->assign('cur_lanmu',4);
        return $this->fetch();

    }

    /**
     * @Author  lingyun
     * @Desc    修改手机号
     * 2018-05-10
     */
    public function edit_mobile(){
        $userservice = new UserService();
        $userotherservice = new UserOtherService();
        if(request()->isPost()){
            $userotherservice->edit_mobile();
        }
        $userservice->get_header();
        $userservice->get_user_info();
        $this->assign('cur_lanmu',4);
        return $this->fetch();
    }

    /**
     * @Author  lingyun
     * @Desc    获取短信验证码
     * 2018-05-10
     */
    public function get_msg_code(){
        $userotherservice = new UserOtherService();
        $userotherservice->get_msg_code();
    }




}
